package com.xinnet.mms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.customerManage.service.CustomerManageService;
import com.xinnet.mms.dept.service.DeptService;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.PushResults;
import com.xinnet.mms.entity.Role;
import com.xinnet.mms.login.service.EmpService;
import com.xinnet.mms.role.service.RoleService;
import com.xinnet.mms.utils.AgainPageView;

@Controller
@RequestMapping(value = "/authority/employee")
public class EmpController {
	@Autowired
	EmpService empService;
	@Autowired
	DeptService deptService;
	@Autowired
	RoleService roleService;
	
	@Autowired
	CustomerManageService customerManageService;
	/**
	 * 员工页面
	 * @param request	 * @param response
	 * @return employee.html 员工页面
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/empPage")
	public String empPage(HttpServletRequest request,HttpServletResponse response){
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		String applyerId = currentUser.getEmpno();
		String deptId = currentUser.getId();
		String deptIdStr = deptService.getChildNodeId(deptId);
		List<Emp> empList = empService.getEmpListByDeptIdStrOrNoDeptRoleId(deptIdStr);
		
		
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		List<Dept> deptList = deptService.getDeptList();
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("deptList", deptList);
		//String deptJson  = deptService.getDeptTree();
		String deptJson  = "["+deptService.getDeptTreeByDeptId(deptId)+"]";
		request.setAttribute("deptJson", deptJson);
		request.setAttribute("roleList", roleList);
		request.setAttribute("pageView", pageView);
		request.setAttribute("isQuery", false);
		return "/authority/employee";
	}
	/**
	 * 账户管理
	 * @param request
	 * @param response
	 * @return employee.html 员工页面
	 * @throws Exception 
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor=Exception.class)
	@RequestMapping(value = "/updateEmpAccount")
	public String updateEmpAccount(HttpServletRequest request,HttpServletResponse response){
		String empno = request.getParameter("empno");
		String loginName = request.getParameter("loginName");
		String plainPassword = request.getParameter("password");
		Emp emp = new Emp();
		emp.setEmpno(empno);
		emp.setLoginName(loginName);
		emp.setPlainPassword(plainPassword);
		empService.updateEmpAccount(emp);
		return "/authority/employee";
	}
	/**
	 * 角色修改
	 * @param request
	 * @param response
	 * @return employee.html 员工页面
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/updateEmpRole")
	public String updateEmpRole(HttpServletRequest request,HttpServletResponse response){
		String empno = request.getParameter("empno");
		String deptId = request.getParameter("deptId");
		String roleId = request.getParameter("roleId");
		String isResponsible = request.getParameter("isResponsible");
		if(!"1".equals(isResponsible)){
			isResponsible = "0";
			
		}
		Emp emp = new Emp();
		emp.setEmpno(empno);
		emp.setId(deptId);
		emp.setRolId(roleId);
		emp.setIsResponsible(Integer.parseInt(isResponsible));
		empService.updateEmpRole(emp);
		return "/authority/employee";
	}
	/**
	 * 批量角色修改
	 * @param request
	 * @param response
	 * @return employee.html 员工页面
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/bathUpdateEmpRole")
	public String bathUpdateEmpRole(HttpServletRequest request,HttpServletResponse response){
		String empnoStr = request.getParameter("empnoStr");
		String deptId = request.getParameter("deptId");
		String roleId = request.getParameter("roleId");
		String isResponsible = request.getParameter("isResponsible");
		empService.bathUpdateEmpRole(empnoStr,deptId,roleId,isResponsible);
		return "/authority/employee";
	}
	/**
	 * 获取员工列表
	 * @param request
	 * @param response
	 * @return employee.html 员工页面
	 * @throws UnsupportedEncodingException 
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getEmpList")
	public String getEmpList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
		request.setCharacterEncoding("utf-8");
		String ename = request.getParameter("ename");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		String roleId = request.getParameter("roleId");
		String deptId = request.getParameter("deptId");
		Emp emp = new Emp();
		if(ename!=null && !"".equals(ename)){
			emp.setEname(ename);
		}
		if(roleId!=null && !"".equals(roleId)){
			emp.setRolId(roleId);
		}
		if(phone!=null && !"".equals(phone)){
			emp.setPhone(phone);
		}
		if(email!=null && !"".equals(email)){
			emp.setEmail(email);
		}
		if(deptId!=null && !"".equals(deptId)){
			String deptIdStr = deptService.getChildNodeId(deptId);
			emp.setDeptIdStr(deptIdStr);
		}else{
			Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
			String tempDeptId = currentUser.getId();
			String deptIdStr = deptService.getChildNodeId(tempDeptId);
			emp.setDeptIdStr(deptIdStr);
			
		}
		List<Emp> empList = empService.getEmpListByParamOrNoDeptRoleId(emp);
		String pageNow = request.getParameter("pageNow");
		PageView pageView = new PageView();
		AgainPageView againPageView = new AgainPageView();
		if(empList!=null){
			pageView.setPageSize(10);
			pageView.setQueryResult(empList.size(), empList);
		}
		if(pageNow==null || "".equals(pageNow)){
			againPageView.againComputePageNowRecords(pageView,1,empList);
		}else{
			againPageView.againComputePageNowRecords(pageView,Integer.parseInt(pageNow),empList);
		}
		List<Dept> deptList = deptService.getDeptList();
		List<Role> roleList = roleService.getRoleList();
		request.setAttribute("isQuery", true);
		request.setAttribute("pageView", pageView);
		if(!"".equals(roleId)){
			request.setAttribute("roleId", roleId);
		}
		if(!"".equals(deptId)){
			request.setAttribute("deptId", deptId);
		}
		if(!"".equals(ename)){
			request.setAttribute("ename", ename);
		}
		Dept dept = deptService.getDeptByDeptId(deptId);
		request.setAttribute("dept",dept);
		request.setAttribute("phone",phone);
		request.setAttribute("email",email);
		//String deptJson  = deptService.getDeptTree();
		Emp currentUser = (Emp)request.getSession().getAttribute("currentUser");
		deptId = currentUser.getId();
		String deptJson  = "["+deptService.getDeptTreeByDeptId(deptId)+"]";
		request.setAttribute("deptJson", deptJson);
		request.setAttribute("deptList", deptList);
		request.setAttribute("roleList", roleList);
		
		return "/authority/employee";
		
	}
	/**
	 * 
	 *
	 * 功能描述：清空销售时间区间的可推送个数
	 * 
	 * @author 湛智
	 * @throws IOException 
	 *
	 * @throws 2014年8月6日
	 *
	 */
	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/clearSurplusLimit")
	public String clearSurplusLimit(HttpServletRequest request,HttpServletResponse response,String timeUp,String timeDown) throws IOException{
		PrintWriter out=response.getWriter();
		List<Emp> empList=empService.getAllEmpList();
		for(Emp emp:empList) {
			try {
				
				PushResults pushResults = new PushResults();
				pushResults.setEmpId(emp.getEmpno());
				pushResults.setTimeUp(timeUp);
				pushResults.setTimeDown(timeDown);
				List<PushResults> list = customerManageService.getPushNumber(pushResults);
				if(list==null || list.isEmpty()) {
					continue;
				}
				emp.setSurplusLimit(emp.getSurplusLimit()-list.size());
				empService.updateSurplusLimit(emp);
			
			} catch (Exception e) {
				e.printStackTrace();
				out.print(2);
				return null;
			}
		}
		out.print(1);
		return null;
	}
	

	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getEmpListByDeptIdStr")
	public List<Emp> getEmpListByDeptIdStr(String deptIdStr){
		return empService.getEmpListByDeptIdStr(deptIdStr);
	}
	
	@ResponseBody
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/getEmpbyLoginUser")
	public List<Emp> getEmpbyLoginUser(HttpSession session){
		Emp currentUser = (Emp) session.getAttribute("currentUser");
		if (null == currentUser) {
			return null;
		}
		String roleid = currentUser.getRolId();
		//后续添加获取分公司经理角色的id，暂时写死
		if("0a070a0e-dd92-42ab-a025-7e19d7ef34de".equals(roleid)||"分公司总监".equals(roleid)){
			return empService.getEmpListByDeptIdStrOrNoDeptRoleId(currentUser.getId());//根据直销部id获取员工列表
		}
		return null;
	}
	
}
